//
//  WHVisitorLoginView.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/6.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit
import SnapKit

//声明协议
@objc protocol WHVisitorLoginViewDelegate : NSObjectProtocol{
    //声明方法
    optional func userWillLogin()
    optional func userWllResgister()
}


class WHVisitorLoginView: UIView {

    //声明代理的时候考虑循环引用 用weak
    //swift中属性默认是strong
    weak var visitorDelegate: WHVisitorLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置方法
    func setVisitorInfo(imageName: String,title: String,isHome: Bool = false){
        if isHome {
            //圆圈转动
            self.startAnimation()
        }else{
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
        }
        tipLabel.text = title
        circleIcon.image = UIImage(named: imageName)
    }
    
    
    //setupUI
    private func setupUI(){
        
        addSubview(circleIcon)
        addSubview(backImage)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)

        //约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            //make就是circle取消了frame布局的对象
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
           make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom)
            make.width.equalTo(100)
            
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom)
            make.width.equalTo(100)
        }
        
        backImage.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(circleIcon.snp_bottom)
            make.centerX.equalTo(circleIcon.snp_centerX)
        }
        
        //背景颜色
        let value: CGFloat = 237/255.0
        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //登录注册点击
        loginBtn.addTarget(self, action: "loginDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        registerBtn.addTarget(self, action: "registerDidClick", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    //登录点击事件
    @objc private func loginDidClick(){
        
        visitorDelegate?.userWillLogin?()
    }
    
    //注册点击事件
    @objc private func registerDidClick(){
        visitorDelegate?.userWllResgister?()
    }
    
    
    
    
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        anim.toValue = 2 * M_PI
        //动画执行结束或者处于非活跃状态 动画不移除图层
        anim.removedOnCompletion = false
        circleIcon.layer.addAnimation(anim, forKey: nil)
        
    }
    
    //懒加载控件
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示文案
    private lazy var tipLabel: UILabel = {
        
        let l = UILabel()
        l.text = "关注一些人,回到这里看看有什么惊喜"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
//        l.preferredMaxLayoutWidth = 230
        l.numberOfLines = 0
        l.textAlignment  = NSTextAlignment.Center
        //自适应大小
        l.sizeToFit()
        return l
        
    }()
    
    //登录 注册按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        //设置背景图片
        let image = UIImage(named: "common_button_white_disable")!
        
        let width = Int(image.size.width * 0.5 )
        let height = Int(image.size.height * 0.5 )
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(width, topCapHeight: height) , forState: UIControlState.Normal)
        
        return btn
    }()
    
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return btn
    }()
}
    
    
    
    
    









